<?php

    /**
     * @package      Modules
     * @subpackage   Admins_Setup
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Admins_Setup_Installer extends Modules_Setup_Installer
    {

    ////////////////////////////////////////////////////////////////////////////
    //                               Attributes                               //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Show module
         *
         * @var     string
         */
        protected $_display = true;

        const DEFAULT_PASSWORD  = 'admin';
        const DEFAULT_EMAIL     = 'admin@example.com';

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Post install
         *
         * @throws       Core_Exception
         * @return       void
         */
    	public function postInstall()
        {
        	parent::postInstall();
        	
        	if (empty($_GET['email']) || !Doctrine_Validator_Email::validate($_GET['email'])) {
        		$password = self::DEFAULT_PASSWORD;
        		$email    = self::DEFAULT_EMAIL;
        	} else {
                $password = DbAdmin::getRandomPassword();
                $email    = $_GET['email'];
        	}
        	
        	$profile             = Doctrine::getTable('DbProfile')->findByName('Admin')->getFirst();
        	$country             = Doctrine::getTable('DbCountry')->findByCode('FR')->getFirst();
        	$language            = Doctrine::getTable('DbLanguage')->findByCode('fr')->getFirst();
        	
            $admin               = new DbAdmin();
            $admin->status       = DbBase::ENABLED;
            $admin->name         = 'Admin';
            $admin->firstname    = 'Admin';
            $admin->login        = 'admin';
            $admin->email        = $email;
            $admin->password     = sha1($password);
            $admin->DbCountry    = $country;
            $admin->DbLanguage   = $language;
            $admin->DbProfile[]  = $profile;
            
            $admin->save();
            
            if ($password != self::DEFAULT_PASSWORD) {
	            $mail   = new Core_Mail_Zend('backend');
	            $mail->setTemplate('install-complete');
	            $mail->setSubject('Installation de Serum');
	            $mail->setFrom(self::DEFAULT_EMAIL);
	            $mail->addTo($admin->email);
	            
	            $mail->setParam('admin',    $admin);
	            $mail->setParam('password', $password);
	            
	            $mail->send();
            }
        }
    }
